import axios from 'axios'
export default {
    namespaced: true,
    state() {
        return {
            goodlist: []
        }
    },
    mutations: {
        getlist(state, list) {
            state.goodlist = list
        },
        getShow(state, val) {
            return state.goodlist.forEach(obj => {
                obj.goods_state = val
            })
        }
    },
    actions: {
        asyncGetList(store) {
            axios({
                url: 'https://www.escook.cn/api/cart'
            }).then(res => {
                store.commit('getlist', res.data.list)
            })
        }
    },
    getters: {
        allPrice(state) {
            return state.goodlist
                .filter((obj) => obj.goods_state == true)
                .reduce((sum, p) => {
                    return sum + p.goods_count * p.goods_price;
                }, 0);
        },
        allCount(state) {
            return state.goodlist
                .filter((obj) => obj.goods_state == true)
                .reduce((sum, p) => sum + p.goods_count, 0);
        },
        ischecked(state) {
            return state.goodlist.every(obj => {
                return obj.goods_state
            })
        }
    }
}